package com.yunfatong.evidence.controller.api;

import com.yunfatong.evidence.common.resultEnum.ResultCode;
import com.yunfatong.evidence.common.validator.ValidatorUtils;
import com.yunfatong.evidence.controller.base.BaseController;
import com.yunfatong.evidence.form.common.BaseApplyForm;
import com.yunfatong.evidence.form.contract.AddContractFileInfoForm;
import com.yunfatong.evidence.form.contract.AddContractInfoForm;
import com.yunfatong.evidence.form.contract.AddContractUserInfoForm;
import com.yunfatong.evidence.model.EvidenceContractFileInfo;
import com.yunfatong.evidence.model.EvidenceContractInfo;
import com.yunfatong.evidence.model.EvidenceContractUserInfo;
import com.yunfatong.evidence.service.EvidenceContractUserInfoService;
import com.yunfatong.evidence.service.UserMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 合同相关存证controller
 */
@RestController
@RequestMapping("/manage/evidence/baseEvidence")
public class EvidenceContractRelatedController extends BaseController {

    @Autowired
    private EvidenceContractUserInfoService evidenceContractUserInfoService;

    @Autowired
    private UserMasterService userMasterService;

    /**
     * 存证成功返回值中的key
     */
    private String evidenceId = "evidenceId";

    /**
     * description:新增用户存证接口  (存合同中的人的信息)
     *
     * @return
     * @author liran
     */
    @PostMapping(value = "/addContractUserInfo")
    public Object addContractUserInfo(@RequestBody BaseApplyForm<AddContractUserInfoForm> applyForm) {
        //参数校验
        userMasterService.commonGetUserIdByCode(applyForm, applyForm.getApplyInfo().getClass());
        ValidatorUtils.validateEntity(applyForm, ResultCode.EVIDENCE_CONTRACT_RELATED_ADD_CONTRACT_USER_INFO);

        EvidenceContractUserInfo record = evidenceContractUserInfoService.addContractUserInfo(applyForm.getApplyInfo());
        return getSuccessResult(evidenceId, record.getId());
    }

    /**
     * description: 新增合同文件存证接口 (存合同文件的信息)
     *
     * @return
     * @author liran
     */
    @PostMapping(value = "/addContractFileInfo")
    public Object addContractFileInfo(@RequestBody BaseApplyForm<AddContractFileInfoForm> applyForm) {
        userMasterService.commonGetUserIdByCode(applyForm, applyForm.getApplyInfo().getClass());
        ValidatorUtils.validateEntity(applyForm, ResultCode.EVIDENCE_CONTRACT_RELATED_ADD_CONTRACT_FILE_INFO);

        EvidenceContractFileInfo record = evidenceContractUserInfoService.addContractFileInfo(applyForm.getApplyInfo());
        return getSuccessResult(evidenceId, record.getId());
    }


    /**
     * description: 存证表-合同、人和文件关联信息存证接口
     *
     * @return
     * @author liran
     */
    @PostMapping(value = "/addContractInfo")
    public Object addContractInfo(@RequestBody BaseApplyForm<AddContractInfoForm> applyForm) {
        userMasterService.commonGetUserIdByCode(applyForm, applyForm.getApplyInfo().getClass());
        ValidatorUtils.validateEntity(applyForm, ResultCode.EVIDENCE_CONTRACT_RELATED_ADD_CONTRACT_INFO);

        EvidenceContractInfo record = evidenceContractUserInfoService.addContractInfo(applyForm.getApplyInfo());
        return getSuccessResult(evidenceId, record.getId());
    }


}
